package com.shanghaionstar.carmanage.service;

import android.content.Context;

import com.onstar.cn.api.v3.PayChannelService;
import com.onstar.cn.api.v3.UserService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.smart.entity.PayChannelRequestEntity;
import com.onstar.cn.smart.entity.PayChannelResponseEntity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by lilongmin on 17/7/4.
 * 请求支付渠道
 */
public class PaymentChannelService extends BaseService<PayChannelResponseEntity> {
    private Context context;

    public PaymentChannelService(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public RequestEntity getRequestEntity() {
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        UserService publicService = new UserService(Constants.CURRENT_SERVER_URL);
        requestEntity = publicService.getPresonalInfomation(requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }


    @Override
    public PayChannelResponseEntity jsonToBean(String json) {
        return GsonHelper.fromJson(json, PayChannelResponseEntity.class);
    }

    public Observable<String> init() {
        return Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                return Observable.just("init");
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<PayChannelResponseEntity> paymentChannelList(final String pageNumber, final String pageSize) {
        return Observable.defer(new Func0<Observable<PayChannelResponseEntity>>() {
            @Override
            public Observable<PayChannelResponseEntity> call() {
                PayChannelService publicService = new PayChannelService(Constants.CURRENT_SERVER_URL);
                RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
                coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
                requestEntity.setCustomHeads(coHashMap);
                //
                PayChannelRequestEntity channelRequestEntity = new PayChannelRequestEntity();
                channelRequestEntity.setIsAvailable("1");//“1：可用、0不可用”
                requestEntity = publicService.getPaymentChannelList(requestEntity, channelRequestEntity, pageNumber, pageSize);
                //
                requestEntity = HttpInjectUtils.injectHttp(PaymentChannelService.this.getClass(), "paymentChannelList", requestEntity, String.class, String.class);

                String jsonStr = null;
                try {
                    jsonStr = BusinessUtil.getResponse(getDatas(requestEntity), context);
                } catch (OnStarException e) {
                    e.printStackTrace();
                }
                return Observable.just((PayChannelResponseEntity) GsonHelper.fromJson(jsonStr, PayChannelResponseEntity.class));
            }
        });
    }
}
